﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.DynamicData.ModelProviders;
using System.Collections.ObjectModel;
using NHibernate.Linq;

namespace DD.NHibernateProvider
{
    public class NHibernateDataModelProvider : DataModelProvider
    {
        private List<TableProvider> tables = new List<TableProvider>();

        public NHibernateDataModelProvider()
        {
            this.ContextType = typeof(GenericNHibernateContext);

            foreach (var item in NHibernateFactory.Current.Configuration.ClassMappings)
            {
                tables.Add(new NHibernateTableProvider(this, item));
            }
        }

        public override object CreateContext()
        {
            return new GenericNHibernateContext();
        }

        public override ReadOnlyCollection<TableProvider> Tables
        {
            get { return tables.AsReadOnly(); }
        }
    }
}
